<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="用户昵称" prop="userName">
        <el-input v-model.trim="form.userName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input v-model.trim="form.phone" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model.trim="form.password" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { doEdit } from '@/api/userManagement'
  import { Register, updateUserInfo } from '@/api/user'

  export default {
    name: 'UserManagementEdit',
    data() {
      return {
        form: {
          userName: '',
          phone: '',
          roleId: '5',
          wxId: '',
          password: '',
        },
        rules: {
          userName: [
            { required: true, trigger: 'blur', message: '请输入用户昵称' },
          ],
          phone: [
            { required: true, trigger: 'blur', message: '请输入手机号' },
            {
              pattern: /^1[3|5|7|8|9]\d{9}$/,
              message: '请输入正确的号码格式',
              trigger: 'change',
            },
          ],
          password: [
            { required: true, trigger: 'blur', message: '请输入密码' },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加管理员'
        } else {
          this.title = '编辑管理员'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.title == '添加管理员') {
              const { data } = await Register(this.form)
              if (data.status === 0) {
                this.$baseMessage(data.message, 'success')
                this.$emit('fetch-data')
                this.close()
              } else {
                this.$baseMessage(data.message, 'error')
              }
            } else {
              const { data } = await updateUserInfo(this.form)
              if (data.status === 0) {
                this.$baseMessage(data.message, 'success')
                this.$emit('fetch-data')
                this.close()
              } else {
                this.$baseMessage(data.message, 'error')
              }
            }
          } else {
            return false
          }
        })
      },
    },
  }
</script>
